var config = require('../../utils/config.js');
var util = require('../../utils/util.js');
var demandTools = require('../../utils/demand.js');
var API_URL = config.API_URL;
var app = getApp();
var page, loading, news_page, news_loading;
let getData = (that) => {
    if (loading) {
        return
    }
    loading = true;
    // 发送请求拿到数据
    let url = API_URL.demand_list;
    let params = { page: page };
    util.PRL(url, params).then(res => {
        console.log(res);
        let data = res.data.data.demands;
        // let data = res.data.data.demands;
        console.log(data)
        let demands;
        if (page == 1) {
            demands = []
        } else {
            demands = that.data.damands;
        }
        demands = demands.concat(data);
        that.setData({
            demands: demands,
            count: res.data.data.count
        })
        if (data.length != 0) {
            page++
        }
        loading = false;
        my.stopPullDownRefresh();
    }).catch(res => {
        loading = false;
        console.log(res);
        my.stopPullDownRefresh();
    })
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hidden: true,
        new_icon: 'down',
        showRecommend: false,
        count: {
            newDemandRecommendCount: 0,
            newDemandInviteCount: 0
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        page = 1;
        getData(this);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        loading = false;
        let isTGX = my.getStorageSync({ key: 'isTGX' }).data;
        let user = my.getStorageSync({ key: "user" });
        // let getLoginStatus = my.getStorageSync({ key: "isTGX" }).data;
        if (isTGX) {
            that.setData({
                showRecommend: true
            })
        } else {
            that.setData({
                showRecommend: false
            })
        }
        this.setData({
            isTGX: isTGX,

        })
        if (app.globalData.refresh) {
            console.log('refresh');
            app.globalData.refresh = false;
            page = 1;
            getData(this)
        }
    },
    onPullDownRefresh: function () {
        page = 1;
        getData(this);
    },
    // 显示推送  推荐的详情
    change: function (e) {
        let that = this;
        let new_icon = that.data.new_icon == 'up' ? 'down' : 'up';
        let hidden = !that.data.hidden;
        that.setData({
            hidden: hidden,
            new_icon: new_icon
        })
        if (new_icon == 'up') {
            news_page = 1;
            news_loading = true;
            util.PRL(API_URL.demand_msg, { page: news_page })
                .then(res => {
                    console.log(res)
                    that.setData({
                        news: res.data.data
                    })
                    if (res.data.data.length != 0) {
                        news_page++;
                    }
                    news_loading = false;
                }).catch(res => {
                    console.log(res);
                    news_loading = false;
                })
        }
    },
    // 蒙层的关闭
    hidden: function (e) {
        this.setData({
            hidden: true,
            new_icon: 'down'
        })
    },
    // 去求购详情
    info: function (e) {
        let that = this;
        let index = e.currentTarget.dataset.index;
        let demands = that.data.demands;
        if (demands[index].handleStatus != '未完成') {
            return
        }
        app.globalData.demand = demands[index];
        my.navigateTo({
            url: '/pages/demand/info/info',
        })
    },
    // 邀请
    addI: function (e) {
        let that = this;
        let index = e.currentTarget.dataset.index;
        let demands = that.data.demands;
        demandTools.addI(demands[index].demandId);
    },
    // 推荐
    addR: function (e) {
        let that = this;
        let index = e.currentTarget.dataset.index;
        let demands = that.data.demands;
        console.log(demands[index].recommends)
        app.globalData.recommends = demands[index].recommends
        demandTools.addR(demands[index].demandId);
    },
    // 添加求购
    add: function (e) {
        my.navigateTo({
            url: '/pages/demand/add/add?action=add',
        })
    },
})